
package user;

import database.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 *
 * @author Jack
 */
public class Search extends SearchGUI implements ActionListener {
    
    public Search(){
        addActionListenersToButtons();
    }

    private void addActionListenersToButtons() {
        btnSearch.addActionListener(this);
        btnRegister.addActionListener(this);
        btnProfile.addActionListener(this);
        
    }
    //Button event listener
    @Override
    public void actionPerformed(ActionEvent e) {
        //if button Search is clicked
        if(e.getSource()==btnSearch){
            refreshSearchList();
        }
        //if button profile is clicked shows profile frame
        if(e.getSource()==btnProfile){
            new Profile().setVisible(true);
        }
        //if button register is clicked show register frame
        if(e.getSource()==btnRegister){
            new Register().setVisible(true);
        }
    }

    private void refreshSearchList() {
        ArrayList<User> listOfUsers=null;
        Database.connect();
        
        //get search selection (Name,Address or Phone) and query database
        int selectedItem = cbSearchSelection.getSelectedIndex();
        if(selectedItem==0){
            listOfUsers=Database.getUsersByName(etSearch.getText());
        }else 
        if(selectedItem==1){
            listOfUsers=Database.getUsersByAdress(etSearch.getText());
        }else
        if(selectedItem==2){
            listOfUsers=Database.getUsersByPhone(etSearch.getText());
        }
        Database.shutdown();
        
        //populating SearchList with results
        if(listOfUsers!=null){
            searchList.setModel(new SearchListPopulate(listOfUsers));
        }
    }
   
}
